var async = require('async');
var path = require('path');
var fs = require('fs');
var navigator = './api/navigator.json';
var articles = './api/articles.json';

exports.index = function (req, res) {
    async.parallel([
        function (cb) {
            var _navigator = JSON.parse(fs.readFileSync(navigator));
            cb(null, _navigator);
        },
        function (cb) {
            var _articles = JSON.parse(fs.readFileSync(articles));
            cb(null, _articles)
        }
    ], function (error, results) {
        var navigator = results[0],
            articles = results[1];
        res.render('index', {
            navigator: navigator,
            articles: articles
        });
    });
}

exports.post = function (req, res) {
    async.parallel([
        function (cb) {
            var _navigator = JSON.parse(fs.readFileSync(navigator));
            cb(null, _navigator);
        }
    ], function (error, results) {
        var navigator = results[0];
        res.render('post', {
            navigator: navigator
        });
    });
}

exports.editor = function (req, res) {
    async.parallel([
        function (cb) {
            var _navigator = JSON.parse(fs.readFileSync(navigator));
            cb(null, _navigator);
        }
    ], function (error, results) {
        var navigator = results[0];
        res.render('editor', {
            navigator: navigator
        });
    });
}

exports.article = function (req, res) {
    var params = req.params, id=params.id;
    async.parallel([
        function (cb) {
            var _navigator = JSON.parse(fs.readFileSync(navigator));
            cb(null, _navigator);
        },
        function (cb) {
            var _articles = JSON.parse(fs.readFileSync(articles));
            cb(null, _articles)
        }
    ], function (error, results) {
        var navigator = results[0],
            article = results[1][id];
        res.render('article', {
            navigator: navigator,
            article: article
        });
    });
}